//
//  BIDMenuView.m
//  商务厅
//
//  Created by mal on 13-12-11.
//  Copyright (c) 2013年 mal. All rights reserved.
//

#import "BIDMenuView.h"

/**
 *菜单未按下时的背景色
 */
#define NORMAL_COLOR [UIColor colorWithRed:73.0f/255.0f green:72.0f/255.0f blue:75.0f/255.0f alpha:1.0f]
#define PRESS_COLOR [UIColor colorWithRed:66.0f/255.0f green:65.0f/255.0f blue:67.0f/255.0f alpha:1.0f]
#define LINE_COLOR [UIColor colorWithRed:83.0f/255.0f green:82.0f/255.0f blue:84.0f/255.0f alpha:1.0f]

@interface BIDMenuView()
{
    NSArray *_menuNameArr;
    NSArray *_imgNameArr;
    BIDMenuAttribute _menuAttribute;
}
@end

@implementation BIDMenuView
@synthesize btnsArr;
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame menuNameArr:(NSArray *)menuNameArr imgNameArr:(NSArray *)imgNameArr menuAttribute:(BIDMenuAttribute)menuAttribute
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor clearColor]];
        if(menuNameArr)
        {
            _menuNameArr = [NSArray arrayWithArray:menuNameArr];
        }
        if(imgNameArr)
        {
            _imgNameArr = [NSArray arrayWithArray:imgNameArr];
        }
        //if(menuAttribute)
        {
            _menuAttribute = menuAttribute;
        }
        btnsArr = [[NSMutableArray alloc] initWithCapacity:[menuNameArr count]];
        [self initView];
    }
    return self;
}

- (void)initView
{
    int topSpacing = _menuAttribute.triangleHeight;
    int bottomSpacing = 0;
    int leftSpacing = 15;
    int interval = 1;
    UIButton *btn;
    CGSize btnSize = CGSizeMake(_menuAttribute.menuWidth, 45);
    for(int i=0; i<_menuNameArr.count; i++)
    {
        CGRect btnFrame;
        CGRect lineFrame;
        if(i==0)
        {
            btnFrame = CGRectMake(leftSpacing, topSpacing, btnSize.width-leftSpacing*2, btnSize.height);
        }
        else
        {
            btn = (UIButton*)[btnsArr objectAtIndex:i-1];
            btnFrame = CGRectMake(leftSpacing, btn.frame.origin.y+btn.frame.size.height+interval+1, btnSize.width-leftSpacing*2, btnSize.height);
        }
        btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = btnFrame;
        btn.tag = i;
        UIImage *img = [UIImage imageNamed:_imgNameArr[i]];
        [btn setImage:img forState:UIControlStateNormal];
        [self setButtonAttribute:btn text:[_menuNameArr objectAtIndex:i]];
        [self addSubview:btn];
        [btnsArr addObject:btn];
        //
        if(i<_menuNameArr.count-1)
        {
            lineFrame = CGRectMake(0, CGRectGetMaxY(btn.frame)+1, _menuAttribute.menuWidth, 1);
            UILabel *label = [[UILabel alloc] initWithFrame:lineFrame];
            [label setBackgroundColor:LINE_COLOR];
            [self addSubview:label];
        }
    }
    
    CGRect ownFrame = self.frame;
    ownFrame.size.height = btnSize.height * btnsArr.count + 2*interval * (btnsArr.count-1) + topSpacing + bottomSpacing;
    self.frame = ownFrame;
}

- (void)setButtonAttribute:(UIButton *)btn text:(NSString *)text
{
    btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    [btn setTitle:text forState:UIControlStateNormal];
    [btn setBackgroundColor:NORMAL_COLOR];
    [btn addTarget:self action:@selector(btnDownHandler:) forControlEvents:UIControlEventTouchDown];
    [btn addTarget:self action:@selector(btnUpInsideHandler:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(btnUpOutsideHandler:) forControlEvents:UIControlEventTouchCancel];
}

- (void)btnDownHandler:(UIButton *)btn
{
    [btn setBackgroundColor:PRESS_COLOR];
}

- (void)btnUpInsideHandler:(UIButton*)btn
{
    [btn setBackgroundColor:NORMAL_COLOR];
    [delegate selectMenuAtIndex:btn.tag];
    [self removeFromSuperview];
}

- (void)btnUpOutsideHandler:(UIButton *)btn
{
    [btn setBackgroundColor:NORMAL_COLOR];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGPoint originPt = CGPointMake(0, 15);
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height-originPt.y;
    CGFloat triangleWidth = self.frame.size.width/2/4;

    CGFloat radius = 6;
    //起始点
    CGPoint startPt = CGPointMake(width/2, originPt.y);

    CGPoint arc1FromPt = CGPointMake(0, originPt.y);
    CGPoint arc1ToPt = CGPointMake(0, arc1FromPt.y+height/2);
    CGPoint arc2FromPt = CGPointMake(0, arc1FromPt.y+height);
    CGPoint arc2ToPt = CGPointMake(width/2, arc1FromPt.y+height);
    CGPoint arc3FromPt = CGPointMake(width, arc1FromPt.y+height);
    CGPoint arc3ToPt = CGPointMake(width, arc1FromPt.y+height/2);
    CGPoint arc4FromPt = CGPointMake(width, originPt.y);
    CGPoint arc4ToPt = CGPointMake(width-width/8, originPt.y);
    CGPoint pt1 = CGPointMake(arc4ToPt.x-triangleWidth/2, 0);
    CGPoint pt2 = CGPointMake(pt1.x-triangleWidth/2, originPt.y);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 0.5);
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextMoveToPoint(context, startPt.x, startPt.y);
    CGContextAddArcToPoint(context, arc1FromPt.x, arc1FromPt.y, arc1ToPt.x, arc1ToPt.y, radius);
    CGContextAddArcToPoint(context, arc2FromPt.x, arc2FromPt.y, arc2ToPt.x, arc2ToPt.y, radius);
    CGContextAddArcToPoint(context, arc3FromPt.x, arc3FromPt.y, arc3ToPt.x, arc3ToPt.y, radius);
    CGContextAddArcToPoint(context, arc4FromPt.x, arc4FromPt.y, arc4ToPt.x, arc4ToPt.y, radius);
    CGContextAddArcToPoint(context, arc4ToPt.x, arc4ToPt.y, pt1.x, pt1.y, 0);
    CGContextMoveToPoint(context, startPt.x, startPt.y);
    CGContextAddLineToPoint(context, pt2.x, pt2.y);
    CGContextAddLineToPoint(context, pt1.x, pt1.y);
    CGContextAddLineToPoint(context, arc4ToPt.x, arc4ToPt.y);
    CGContextSetFillColorWithColor(context, NORMAL_COLOR.CGColor);
    CGContextDrawPath(context, kCGPathFillStroke);
}
//- (void)drawRect:(CGRect)rect
//{
//    int menuWidth = rect.size.width;
//    int menuHeight = rect.size.height;
//    int triangleWidth = _menuAttribute.triangleWidth;
//    int triangleHeight = _menuAttribute.triangleHeight;
//    int triangleToRight = _menuAttribute.triangleToRight;
//    CGPoint pt1 = CGPointMake(0, triangleHeight);
//    CGPoint pt2 = CGPointMake(menuWidth-triangleToRight-triangleWidth, triangleHeight);
//    CGPoint pt3 = CGPointMake(menuWidth-triangleToRight-triangleWidth/2, 0);
//    CGPoint pt4 = CGPointMake(menuWidth-triangleToRight, triangleHeight);
//    CGPoint pt5 = CGPointMake(menuWidth, triangleHeight);
//    CGPoint pt6 = CGPointMake(menuWidth, menuHeight);
//    CGPoint pt7 = CGPointMake(0, menuHeight);
//    //
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextSetLineWidth(context, 0.f);
//    //CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
//    CGContextMoveToPoint(context, pt1.x, pt1.y);
//    CGContextAddLineToPoint(context, pt2.x, pt2.y);
//    CGContextAddLineToPoint(context, pt3.x, pt3.y);
//    CGContextAddLineToPoint(context, pt4.x, pt4.y);
//    CGContextAddLineToPoint(context, pt5.x, pt5.y);
//    CGContextAddLineToPoint(context, pt6.x, pt6.y);
//    CGContextAddLineToPoint(context, pt7.x, pt7.y);
//    CGContextSetFillColorWithColor(context, NORMAL_COLOR.CGColor);
//    CGContextDrawPath(context, kCGPathFill);
//}


@end
